package com.tracemap.gpslocation;



import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;




public class GPSManager extends Thread{
	/**
	 * 
	 * */
	private boolean processing;
	static GPSManager gpsMgrInstance;
	/**
	 * Location 
	 * */
	private TraceLocationListener drllListener;
	/**
	 * GPS
	 * */
	private GPSReadListener readListener;
	LocationManager locatonManager;
	boolean firseFixTimeSaved = false;
	private long subscrStart = 0;
	private long timeToFirstFix = 0;
	private String strLocationProvider="";
	
	public static GPSManager getInstance(LocationManager lm)
	{
		if (gpsMgrInstance==null){
			gpsMgrInstance = new GPSManager(lm);
		}	
		return gpsMgrInstance;
	}
	
	private GPSManager(LocationManager lm) {
		
		
		locatonManager=lm;
		drllListener=new TraceLocationListener(locatonManager);
		LocationProvider(locatonManager);
		try {
			locatonManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, drllListener);
			//locatonManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, drllListener);
		}catch (Exception e) {
			Log.e("", e.toString());
			e.printStackTrace();
		}
		
	}
	
	void LocationProvider(LocationManager lm)
	{
		Criteria c =new Criteria();
		c.setAccuracy(Criteria.ACCURACY_FINE);
		c.setAltitudeRequired(false);
		c.setBearingRequired(false);
		c.setCostAllowed(false);
		c.setPowerRequirement(Criteria.POWER_LOW);
		try {
			strLocationProvider = lm.getBestProvider(c, true);
		} catch (Exception e) {
			Log.e("criteria",e.toString());
			e.printStackTrace();
		}
		
	}
	public boolean isProcessing() {
		return processing;
	}
	public void stopGPS()
	{
		drllListener.stopTrace();
		processing = false;
	}
	public void closeGPS()
	{
		readListener = null;
	}
	
	/**
	 * 
	 * */
	@Override
	public void run() {
		processing = true;

		while (isProcessing()) {
			Location l = drllListener.waitForLocation();
			
//			if (l.hasAccuracy()) {
				// l.hasBearing();
				if (!firseFixTimeSaved) {
					setTimeToFirstFix(System.currentTimeMillis() - subscrStart);
					firseFixTimeSaved = true;
				}
				LocationToDated(l);
//			} else {
//
//				if (readListener != null) {
//					readListener.TimeOut();
//				}
//
//			}
		}
		if (readListener != null) {
			readListener.choseDevices();
		}
		System.gc();
		Log.i("GPSManager","exit");
	}

	/**
	 * 
	 * */
	public void setReadListener(GPSReadListener readListener) {
		this.readListener = readListener;
	}

	/**
	 * 
	 * */
	void LocationToDated(Location location) {
		if (location != null) {
			double tempLat = location.getLatitude();
			double tempLng = location.getLongitude();
			double tempAlt = location.getAltitude();

			if(readListener!=null)
			{
				readListener.read(tempLat, tempLng, tempAlt);
			}
			Log.d("Lat", String.valueOf(tempLat));
			Log.d("Lng", String.valueOf(tempLng));

		}
	}

	public void setTimeToFirstFix(long timeToFirstFix) {
		this.timeToFirstFix = timeToFirstFix;
	}

	public long getTimeToFirstFix() {
		return timeToFirstFix;
	}
}
